<?php
session_save_path("/tmp");
session_start();
/*
 *      zendo.php
 *
 *      Copyright 2010 Nolan Baker <hendersonhasselbalch@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
?>


<?php

include("func.php");
$con = mysql_connect("localhost", "jjenning", "");
mysql_query("use zendo;", $con);

if ($_POST){
    if (isset($_POST['name'])){
        # the player starts off with 0 guessing stones
        $_SESSION['stones'] = 0;

        # and is on the first word
        $_SESSION['order'] = 0;

        # choose a rule
        $result = mysql_query("SELECT rule_id FROM rules ORDER BY Rand() LIMIT 1;", $con);
        while ($row = mysql_fetch_array($result)) {
            $_SESSION['rule'] = $row['rule_id'];
            mysql_query("INSERT INTO games (player_name, rule_id, date, time) VALUES ('"
                        .$_POST['name']."',".$row['rule_id'].", CURDATE(), CURTIME());", $con);
        }

        # then store the current game id as session data
        $game_id = mysql_query("SELECT MAX(game_id) as g FROM games;", $con);
        while ($row = mysql_fetch_array($game_id)) {
            $_SESSION['game'] = $row['g'];
        }

        # for now we'll just choose 2 random words
        # randWord should take in 2 args (rule, buddha nature)
        $b = randWord($_SESSION['rule'], 1);
        addWord($b,1);
        $n = randWord($_SESSION['rule'], 0);
        addWord($n,0);
        

    }

########################################################################
# ADD WORD
########################################################################
    $errors = "";
    if (isset($_POST['type'])) {
        if ($_POST['type'] == 'add') {
            $word = strtolower($_POST['word']);
            $is_word = isWord($word);
            if (! $is_word) {
                $errors .= "You must use an English word.\n";
            }
            elseif (! isset($_POST['buddha'])) {
                $errors .= "You must select either 'buddha' or 'not buddha'.\n";
            }

            # check to see if the word has been used
            elseif (used($word, $_SESSION['game'])) {
                $errors .= "You've already used that word.\n";
            }
            else {
                $_SESSION['order'] += 1;
                $nature = isBuddha($word, $_SESSION['rule']);
                $correct = 0;
                if ($nature == $_POST['buddha']) {
                    $correct = 1;
                    $_SESSION['stones'] += 1;
                }
                addWord($word, $nature, $correct, $_SESSION['order']);
            }
        } else if ($_POST['type'] == 'guessPost') {
		    $rule = '';
		    
		    if ($_POST['rule'] == "hasSubword") {
		        $rule = 'r4';
		    } else if ($_POST['rule'] == 'beginsAndEnds') {
		        $rule = 'r2';
		    } else if ($_POST['rule'] == 'palindrome') {
		        $rule = 'r3';
		    } else if ($_POST['rule'] == 'doubleLetter') {
		        $rule = 'r1';
		    } else if ($_POST['rule'] == 'lastLetterLastHalf') {
		        $rule = 'r5';
		    } else if ($_POST['rule'] == 'distinctVowels') {
		        $rule = 'r6';
		    } else if ($_POST['rule'] == 'multipleOf3') {
		        $rule = 'r7';
		    } else if ($_POST['rule'] == 'startsWithS') {
		        $rule = 'r0';
		    }
		    $win = true;
		    foreach ($_POST as $key => $value) {
		        if ($key == 'rule') {
		            continue;
		        }
		        $query = "select " . $rule . " from words where word = '" . $key . "';";
		        $result = mysql_query($query, $con);
		        $result_array = mysql_fetch_array($result)
		        if ($result_array[$rule] != $value) {
		            $win = false;
		            break;
		        }
		    }
		    
		    if ($win) {
		        echo json_encode(array("game_status" => "win!"));
		    }
    	} else if ($_POST['type'] == 'guessGet') {
    		$_SESSION['stones'] = $_SESSION['stones'] - 1;
		
		    # list some words
		    $query = "select word from words where " . $_SESSION['rule'] " = TRUE order by rand() limit 8";
		    
		    $result = mysql_query($query, $con);
		    $word_list = null;
		    
		    while ($row = mysql_fetch_array($result)) {
		        array_push($word_list, $row['word']);
		    }
		    
		    $json_array = json_encode(array("words" => $word_list));
		    
		    echo $json_array;
    	}
    }
}

########################################################################
# TABLE OF WORDS
########################################################################
# buddha words
$array = mysql_query("SELECT word FROM word_game WHERE game_id = "
                     .$_SESSION['game']." AND buddha = 1;", $con);

# Here we need to construct an array of buddha words which will be put into a JSON array
$buddha_words = array();

while ($row = mysql_fetch_array($array)) {
    array_push($buddha_words, $row['word']);
}

$nonbuddha_words = array();

# non-buddha words
$array = mysql_query("SELECT word FROM word_game WHERE game_id = "
                     .$_SESSION['game']." AND buddha = 0;", $con);

# Here we need to construct array of non-buddha words which will be put into a JSON array
while ($row = mysql_fetch_array($array)) {
    array_push($nonbuddha_words, $row['word']);
}

########################################################################
# GUESSING STONES
########################################################################

# THE JSON MAGICAL ARRAY OF WONDER AND MERRIMENT

$json_array = json_encode(array("game_id" => $_SESSION["game"], "buddha" => $buddha_words, "nonbuddha" => $nonbuddha_words, "stones" => $_SESSION['stones'], "errors" => $errors));

echo $json_array;

?>
